import {
  mergeOptions,
  convertColorFormat,
  pickWorldPositionByMouse,
  vec3ToJson,
  jsonToVec3,
} from "../core/common.js";
import global from "../core/global.js";

const Vertical = {
  center: "CENTER_V",
  bottom: "BOTTOM",
  baseLine: "BASELINE",
  top: "TOP",
};
const Horizontal = {
  center: "CENTER_H",
  left: "LEFT",
  right: "RIGHT",
};

let collection = null;

let Label3D = {
  add(options, cb) {
    let defaults = {
      position: null,
      text: "",
      iconUrl: "", //图片
      frameUrl: "", //边框图
      mix: false,
      objectName: "",
      style: {
        font: "Microsoft YaHei",
        fontSize: 12,
        color: "#fff",
        strokeColor: "#000",
        lineToGround: false,
        lineColor: "#ffffff",
        backgroundColor: "#00000000",
        align: "center",
        verticalAlign: "center",
        textAlign: "center",
        textVertical: "center",
        offset: [0, 0],
        iconSize: [20, 20],
        altitude: 0,
        altitudeMethod: "RelativeToTerrain",
        fontBold: false,
        fontItalic: false,
        fontUnderline: false,
      },
    };
    options = mergeOptions(defaults, options);
    if (!options.position) {
      throw new TypeError("position参数需传入坐标");
    }
    const style = options.style;
    let label3d = new SSmap.Label3D();
    let position = jsonToVec3(options.position);

    label3d.position = position;
    if (options.iconUrl) {
      label3d.url = options.iconUrl;
      label3d.imageWidth = style.iconSize[0];
      label3d.imageHeight = style.iconSize[1];
    }
    if (options.frameUrl) {
      label3d.frameUrl = options.frameUrl;
    }
    label3d.objectName = options.objectName;
    label3d.mix = options.mix;

    if (options.text) {
      label3d.text = options.text;
      label3d.font = style.font;
      label3d.fontSize = style.fontSize;
      label3d.fontBold = style.fontBold;
      label3d.fontItalic = style.fontItalic;
      label3d.fontUnderline = style.fontUnderline;
      label3d.textHorizontal =
        SSmap.Horizontal[Horizontal[style.textAlign]];
      label3d.textVertical = SSmap.Vertical[Vertical[style.textVertical]];
    }

    label3d.offset = SSmap.Cartesian2.create(
      style.offset[0],
      style.offset[1]
    );
    label3d.horizontal = SSmap.Horizontal[Horizontal[style.align]];
    label3d.vertical = SSmap.Vertical[Vertical[style.verticalAlign]];

    label3d.setAltitudeMethod(SSmap.AltitudeMethod[style.altitudeMethod]);
    label3d.setAltitude(style.altitude);

    let color = convertColorFormat(style.color);
    label3d.fontColor = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );

    color = convertColorFormat(style.strokeColor);
    label3d.strokeColor = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );

    color = convertColorFormat(style.backgroundColor);
    label3d.background = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );

    if (style.lineToGround) {
      label3d.lineToGround = style.lineToGround;
      color = convertColorFormat(style.lineColor);
      label3d.lineColor = SSmap.Color.fromRgb(
        color.r,
        color.g,
        color.b,
        color.a
      );
    }

    collection = collection
      ? collection
      : SSmap.BillboardCollection.Instance();
    label3d.setCollection(collection);

    let id = global.Label3DStore.add(label3d, options.id);
    cb && cb(id);
    // 在Native中通信网页端和客户端都可以return拿到返回值
    return id;
  },
  pick(options, cb) {
    let scene = GlobalViewer.scene;
    let data = {
      position: { x: 0, y: 0, z: 0 },
      properties: null,
    };
    scene.pickFeature(options.x, options.y).then((feature) => {
      if (feature) {
        let properties = {};
        let label3D = SSmap.Label3D.getSelect(feature);
        if (!label3D) {
          return;
        }
        let nameList = feature.propertyNames();
        let size = nameList.size();

        for (let i = 0; i < size; i++) {
          let key = nameList.get(i);
          let value = feature.getProperty(key);
          properties[key] = value;
        }

        data.properties = properties;
        let point = pickWorldPositionByMouse(options.x, options.y);
        data.position = vec3ToJson(point);
      }
      cb && cb(data);
    });
  },
  changeProperty(id, property) {
    let label3d = global.Label3DStore.get(id);
    if (label3d) {
      for (const key in property) {
        if (Object.hasOwnProperty.call(property, key)) {
          const value = property[key];
          label3d[key] = value;
        }
      }
    }
  },
  _getLabel(id) {
    let label3d = global.Label3DStore.get(id)
    return label3d
  },
  remove(id) {
    global.Label3DStore.has(id) && global.Label3DStore.remove(id);
  },
  removeAll() {
    if (collection) {
      collection.removeAll();
      collection = null;
    }
  },
};
export { Label3D };
